package com.cgsd.heyu.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.BaseAdapter;
import com.android.library.adapter.ClickItemListener;
import com.android.library.adapter.ViewHolder;
import com.cgsd.heyu.R;
import com.cgsd.heyu.entity.ReportEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 举报
 * CreateData: 2020/11/3
 */
public class ReportDialog extends Dialog implements View.OnClickListener, ClickItemListener {
    private Context mContext;
    private BaseAdapter<ReportEntity> adapter;
    private List<ReportEntity> data = new ArrayList<>();
    private int selectedPosition;
    private DialogListener dialogListener;
    private EditText edtContent;

    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }

    public ReportDialog(@NonNull Context context) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        setContentView(R.layout.dialog_report);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        //内容扩展到导航栏
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
        initView();
    }

    private void initView() {
        edtContent = findViewById(R.id.edt_content);
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        this.data.add(new ReportEntity("图片色情、违法、暴力或虚假"));
        this.data.add(new ReportEntity("图片不文明"));
        this.data.add(new ReportEntity("声音不文明"));
        adapter = new BaseAdapter<ReportEntity>(mContext,R.layout.item_report_dialog,data) {
            @Override
            public void convert(ViewHolder holder, ReportEntity reportEntity, int position) {
                ImageView ivSelected = holder.getView(R.id.iv_select);
                holder.setText(R.id.tv_title,reportEntity.cause);
                if (selectedPosition == position){
                    ivSelected.setSelected(true);
                }else {
                    ivSelected.setSelected(false);
                }
            }
        };
        mRecyclerView.setAdapter(adapter);
        adapter.setClickItemListener(this);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_confirm){
            if (dialogListener != null){
                dialogListener.confirm(data.get(selectedPosition),edtContent.getText().toString());
            }
        }else if (v.getId() == R.id.tv_cancel){
            dismiss();
        }
    }

    @Override
    public void clickItem(View view, int position) {
        if (selectedPosition != position){
            selectedPosition = position;
            adapter.notifyDataSetChanged();
        }
    }


    public interface DialogListener{
        /**
         * 确认举报
         * @param entity
         * @param detailContent
         */
        void confirm(ReportEntity entity,String detailContent);
    }
}
